---@class Battle.GrassMgr
local GrassMgr = Battle.CreateClass("GrassMgr")
Battle.GrassMgr = GrassMgr
local this = GrassMgr

function GrassMgr.OnCreate()
    ---@type table<number,Battle.Entity[]>
    this.actorInGrassList = {}
end
function GrassMgr.OnDestroy()

end
---@param actor Battle.Entity
function GrassMgr.LeaveGrass(actor)
    for k,v in pairs(this.actorInGrassList) do
        if v[actor:GetId()] then
            this.SetGrass(actor,k,false)
            return
        end
    end
end
---@param actor Battle.Entity
function GrassMgr.SetGrass(actor,grassId,isIn)
    local grass = this.actorInGrassList[grassId]
    if not grass then
        grass = {}
        this.actorInGrassList[grassId] = grass
    end
    if isIn then
        grass[actor:GetId()] = actor
    else
        grass[actor:GetId()] = nil
    end
    local my = 0
    local enemy = 0
    for k,v in pairs(grass) do
        if v.camp == Battle.CampType.Blue then
            my = my + 1
        elseif v.camp == Battle.CampType.Red then
            enemy = enemy + 1
        end
    end
    if not isIn then
        if actor.effectStateSystem:HasState(Battle.FighterStateEnum.GRASS_HIDE) then
            actor.effectStateSystem:RemoveState(Battle.FighterStateEnum.GRASS_HIDE)
        end
    end
    if my ~=0 and enemy ~= 0 then
        for k,v in pairs(grass) do
            if v.effectStateSystem:HasState(Battle.FighterStateEnum.GRASS_HIDE) then
                v.effectStateSystem:RemoveState(Battle.FighterStateEnum.GRASS_HIDE)
            end
        end
    else
        for k,v in pairs(grass) do
            if not v.effectStateSystem:HasState(Battle.FighterStateEnum.GRASS_HIDE) then
                v.effectStateSystem:AddState(Battle.FighterStateEnum.GRASS_HIDE)
            end
        end
    end
    actor.eventSystem:FireEvent(Battle.Event.ActorEnterGrass,Battle.EventArgs.ActorEnterGrassArgs:Create(actor,grassId,isIn))
end